﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;

namespace MTSGui
{
    public class Startup
    {
        [STAThread]
        [PreEmptive.Attributes.Setup(CustomEndpoint = "so-s.info/PreEmptive.Web.Services.Messaging/MessagingServiceV2.asmx")]
        [PreEmptive.Attributes.Teardown()]
        public static void Main(string[] args)
        {
            if (!Environment.Is64BitOperatingSystem)
            {
                MessageBox.Show("MTS can use large amounts of memory. You may experience difficulty rendering large worlds without a 64-bit operating system.",
                    "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else if (!Environment.Is64BitProcess)
            {
                MessageBox.Show("You have a 64-bit operating system, but MTS is not operating in 64-bit mode. You may experience difficulty rendering large worlds." +
                    "\n\nYou may need to download and install a 64-bit version of the .NET Framework.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            /*else
            {
                if (!Environment.Is64BitProcess)
                {
                    string assemblyPath = Assembly.GetExecutingAssembly().Location;
                    System.Diagnostics.Process.Start(Path.Combine(Directory.GetParent(assemblyPath).ToString(), "64-bit", "mts-gui.exe"));
                    return;
                }
            }*/

            SplashScreen SS = new SplashScreen("Images\\NewSplash.png");
            SS.Show(true, false);

            BlockDataUpdater.UpdateBlockData();

            if (!File.Exists("blockcolors.xml") || !File.Exists("mts.dll"))
            {
                string n = Environment.NewLine;
                MessageBox.Show(
                    "Required files are missing from your MTS installation." + n + n +
                    "Please ensure that you extract all the files in the MTS download into the same folder." + n + n +
                    "The program will now exit."
                    , "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

#if DEBUG
            App app = new App();
            app.InitializeComponent();
            app.Run();
#else
            try
            {
                App app = new App();
                app.InitializeComponent();
                app.Run();
            }
            catch (Exception UncaughtException)
            {
                string n = Environment.NewLine;
                try
                {
                    //Write a crash dump
                    TextWriter ErrorWriter = new StreamWriter("MTSCrash.txt");
                    AssemblyName AppAssembly = Assembly.GetExecutingAssembly().GetName();
                    ErrorWriter.WriteLine("MTS version:       " + AppAssembly.Version.ToString());
                    ErrorWriter.WriteLine("Failure occurred:  " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());

                    ErrorWriter.WriteLine("Operating system:  " + Environment.OSVersion.ToString() + (Environment.Is64BitOperatingSystem ? " (64-bit)" : ""));
                    ErrorWriter.WriteLine("CLR version:       " + Environment.Version.ToString());

                    ErrorWriter.WriteLine(n + "Please report what you did to cause this bug at http://minecraft.codeplex.com/WorkItem/Create, attaching this file to the report." + n);

                    ErrorWriter.WriteLine(UncaughtException.ToString());

                    ErrorWriter.Close();
                }
                catch (Exception ex)
                { }
            }
#endif
        }

        public static bool RIOptIn
        {
            get
            {
                return MTSGui.Properties.Settings.Default.ReportToRI;
            }
        }
    }
}
